package com.ophiux.cs.application.user.impl.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.user.dto.getPatientInfoRequest;
import com.ophiux.cs.application.user.dto.getPatientInfoResponse;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.repositories.IUserRepository;
import com.ophiux.utils.common.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class getUserInfoByPatientService {
	@Autowired
	IUserRepository iUserRepository;
	
	public Result<getPatientInfoResponse> getUserInfoByPatient(getPatientInfoRequest dto){
		Result<getPatientInfoResponse> result = new Result<getPatientInfoResponse>();
		getPatientInfoResponse res = new getPatientInfoResponse();
		try {
			if(!dto.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail); 
			}
			
			User user = iUserRepository.getUserByUserId(dto.getUSER_ID());
			if(user!=null) {
				
				res.setUSER_ID(user.getUSER_ID());
				res.setUSER_NAME(user.getUSER_NAME());
				res.setEMAIL(user.getEMAIL());
				res.setSEX(user.getSEX());
				res.setBIRTH_DAY(user.getBIRTH_DAY());
				String age = DateUtil.getAge(user.getBIRTH_DAY());
				res.setAGE(age);
				res.setADDRESS(user.getADDRESS());
				res.setPHONE(user.getPHONE());
				res.setOAUTH_ID(user.getOAUTH_ID());

//				result.setRESPONSE(res);
				result = Result.success(res);
				
			}else {
				//账号不存在
				return Result.fail(ResultEnum.NotFundUser.getCode(),ResultEnum.NotFundUser.getMsg() );
			}
			
		} catch (Exception e) {
			log.info("getUserInfoByPatient接口异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		
		
		return result;
	}
	
}
